<?php

namespace app\controller\admin\yys;

use app\model\api\YysHero;
use app\model\api\YysJx;
use app\model\api\YysSkill;
use app\model\api\YysSource;
use app\model\api\YysYh;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Yh
 * @package app\controller\admin\yys
 */
class Yh extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new YysYh();
    }


    //添加
    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        if(empty($post['role_id'])){
            throw new \Exception('请选择角色');
        }

        $source = (new YysSource())->where('type','=','yh')
            ->cache(true)->column('ico','name');

        $insData['role_id']  = $post['role_id'];
        $insData['recomm_reason']  = $post['recomm_reason'];
        $insData['main_attr1']     = json_encode([2,4,6]);
        $insData['main_attr2']      = json_encode([$post['main_attr1'],$post['main_attr2'],$post['main_attr3']],JSON_UNESCAPED_UNICODE);
        $insData['ico']    = json_encode(array(
            array(
                'number' => $post['yh1'].'×'.$post['yh1_num'],
                'ico' => $source[$post['yh1']],
            ),
            array(
                'number' => $post['yh2'].'×'.$post['yh2_num'],
                'ico' => $source[$post['yh2']],
            )
        ),JSON_UNESCAPED_UNICODE);
        $insData['create_time'] = time();

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        if(empty($post['role_id'])){
            throw new \Exception('请选择角色');
        }

        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        $source = (new YysSource())->where('type','=','yh')
            ->cache(true)->column('ico','name');

        $row->role_id     = $post['role_id'];
        $row->main_attr1        = json_encode([2,4,6]);
        $row->main_attr2         = json_encode([$post['main_attr1'],$post['main_attr2'],$post['main_attr3']],JSON_UNESCAPED_UNICODE);
        $row->recomm_reason        = $post['recomm_reason'];
        $row->ico = json_encode(array(
            array(
                'number' => $post['yh1'].'×'.$post['yh1_num'],
                'ico' => $source[$post['yh1']],
            ),
            array(
                'number' => $post['yh2'].'×'.$post['yh2_num'],
                'ico' => $source[$post['yh2']],
            )
        ),JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    public function material(){
        $material = (new YysSource())->where('type','=','yh')
            ->select();
        return $this->success('数据获取成功', $material);
    }



    public function info(){
        $id = $this->request->param('id');

        $role = $this->model->where('id',$id)->find()->toArray();
        $material   = json_decode($role['ico'],true);
        $main_attr2   = json_decode($role['main_attr2'],true);
        foreach($material as $k => $v){
            if(strpos($v['number'],'×')){
                $number = explode('×',$v['number']);
                $role['yh'.($k+1)] = $number[0];
                $role['yh'.($k+1).'_num'] = $number[1];
            }else{
                $role['yh'.($k+1)] = $v['number'];
                $role['yh'.($k+1).'_num'] = $v['number'];
            }
        }

        $role['main_attr1'] = $main_attr2[0];
        $role['main_attr2'] = $main_attr2[1];
        $role['main_attr3'] = $main_attr2[2];

        return $this->success('获取成功', $role);
    }

}